package com.sz.mes.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.diboot.core.vo.JsonResult;
import com.sz.mes.entity.Goods;
import com.sz.mes.entity.Material;
import com.sz.mes.entity.TableHead;
import com.sz.mes.mapper.GoodsMapper;
import com.sz.mes.mapper.MaterialMapper;
import com.sz.mes.mapper.TableHeadMapper;
import com.sz.mes.service.MaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 物料相关Service实现
 *
 * @author hem
 * @version 1.0
 * @date 2023-05-19
 * Copyright © szhs
 */
@Service
@Slf4j
public class MaterialServiceImpl extends BaseCustomServiceImpl<MaterialMapper, Material> implements MaterialService {
    private final TableHeadMapper tableHeadMapper;
    private final MaterialMapper materialMapper;
    private final GoodsMapper goodsMapper;

    public MaterialServiceImpl(TableHeadMapper tableHeadMapper, MaterialMapper materialMapper, GoodsMapper goodsMapper) {
        this.tableHeadMapper = tableHeadMapper;
        this.materialMapper = materialMapper;
        this.goodsMapper = goodsMapper;
    }

    @Override
    public JsonResult insert(Material entity) {
        Material material = new Material();
        material.setExcelData(entity.getExcelData());
        try {
            if (material.getExcelData() != null) {
                return dataProcess(material.getExcelData(), material.getId());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return JsonResult.OK("导入数据成功");
    }

    private JsonResult dataProcess(String excelData, Long materialId) {
        //mapList 获取到所有的数据
        ArrayList<Map<String, String>> mapList = new ArrayList<>();


        //hashMap 表头对应的 序号
        HashMap<String, String> hashMap = new HashMap<>();
        //获取表头
        TableHead tableHead = tableHeadMapper.selectById(2);
        String data = tableHead.getData();
        JSONObject jsonObject = JSONObject.parseObject(data);
        for (Object map : jsonObject.entrySet()) {
//            hashMap.put(String.valueOf(((Map.Entry)map).getValue()),String.valueOf(((Map.Entry)map).getKey()));
            hashMap.put(String.valueOf(((Map.Entry) map).getKey()), String.valueOf(((Map.Entry) map).getValue()));
        }

        //获取完表头信息后清楚数据，给其他数据用
        mapList.clear();

        System.out.println("excelData:" + excelData);
        //json转为List
        JSONArray objects = JSONObject.parseArray(excelData);
//        JSONArray objects = JSONObject.parseArray(JSONObject.toJSONString(excelData));
        for (Object object : objects) {
            HashMap<String, String> map1 = JSONObject.parseObject(JSONObject.toJSONString(object), HashMap.class);
            mapList.add(map1);
        }

        System.out.println("mapList:" + mapList);

        for (Map map : mapList) {
            System.out.println(map);
            //物料表插入
            Material material = new Material();
            material.setMaterialNum((String) map.get(hashMap.get("物料编号")));
            material.setMaterialName((String) map.get(hashMap.get("物料名称")));
            material.setSpecifications((String) map.get(hashMap.get("规格")));
            /*Long materialQuantity = Long.valueOf((String) map.get(hashMap.get("物料数量")));
            if (materialQuantity != 0) {
                material.setMaterialQuantity(materialQuantity);
            } else {
                material.setMaterialQuantity(0L);
            }*/
            material.setExcelData(excelData);

            try {
                materialMapper.insert(material);
            } catch (Exception e) {
                return JsonResult.FAIL_OPERATION("插入失败"+ e.getMessage());
            }

            //商品表插入
            Goods goods = new Goods();
            //商品编号就是物料编号
            goods.setGoodsCode((String) map.get(hashMap.get("物料编号")));
            //商品名称就是物料名称
            goods.setGoodsName((String) map.get(hashMap.get("物料名称")));
            //商品规格就是物料规格
            goods.setSpecifications((String) map.get(hashMap.get("规格")));
            goodsMapper.insert(goods);
        }

        return JsonResult.OK("数据导入成功");
    }
}
